﻿//页面方法入口
$(function() {
    //    setMenu(0);
    bindLoinEvent();
    $(".errhide").hide();
    $(".logininput").focus(function() {
        $(".errhide").hide();
    });
    CheckLoginStatus();
});

function login() {
    $("#btnlogin").click();
}

function setMenu(i) {
    $("ul.menu_list>li>a.menu_selected").removeClass('menu_selected');

    var node = $("ul.menu_list>li:eq(" + i + ")>a");
    if (node.length != 0)
        node.addClass("menu_selected");
}

function bindLoinEvent() {
    $("#btnlogin").click(function() {
        var code = '99XB';
        var account = $("#txt_account").val();
        var pwd = $("#txt_password").val();
        var loginMethod = $("#hid_loginmethod").val();

        $(".errorTip1,.errorTip2,.errorTip3").hide();
        if (account.length == 0) {
            //            $("#txt_account").focus();
            $(".errorTip1, .errorTip5").show();
            alert('请输入用户名!');
            return false;
        }
        if (pwd.length == 0) {
            //            $("#txt_password").focus();
            $(".errorTip2, .errorTip6").show();
            alert('请输入密码!');
            return false;
        }
        $(this).hide();
        $('#btnlogining').show();
        var remember = $("#cb_remember").attr("checked") ? 1 : 0;
        $.ajax({
            type: "post",
            url: rootpath + "api/member.ashx",
            data: { type: "dologin", code: code, account: account, password: pwd, rememberpwd: remember, loginmethod: loginMethod },
            dataType: "json",
            success: function(data) {
                if (data.account == undefined) {
                    $(".errorTip3,.errorTip7").show();
                    $('#btnlogining').hide();
                    $("#btnlogin").show();
                    alert('用户名或密码错误!');
                }
                else if (data.redirect != null && data.redirect.length > 0) {
                    //window.location.href = data.redirect;
                    window.location.href = "/portal/bm/default.aspx";
                }
                else {
                    showUserInfo(data);
                    showHeadInfo(data);
                }
            }
        });
        return false;
    });
    bindInputEvent();
}

function bindInputEvent() {
    $("#txt_code").keydown(function() {
        if ($(this).val().length > 0) {
            $(".errorTip4").hide();
        }
    });
    $("#txt_account").keydown(function() {
        if ($(this).val().length > 0) {
            $(".errorTip1").hide();
        }
    });

    $("#txt_password").keydown(function() {
        if ($(this).val().length > 0) {
            $(".errorTip2").hide();
        }
    });
}

function CheckLoginStatus() {
    $.ajax({
        type: "get",
        url: rootpath + "api/member.ashx",
        data: { type: "checklogin", r: Math.random() },
        dataType: "json",
        success: function(data) {
            if (data.memberid != undefined) {
                showUserInfo(data);
            }
            //if(data.isremember){
            //    $("#cb_remember").attr("checked",true);
            $("#txt_account").val(data.account);
            //     $("#txt_password").val(data.password);
            //}
            $("#txt_code").val(data.code);
            if (data.code != data.defaultcode) {
                $("#txt_code").attr("disabled", "disabled");
            }
        }
    });
}

function showUserInfo(data) {
    //    $(".w1024> table").hide();
    $(".Login_k").empty(); 
    var loginBarCss = "loginbar_now";
    //var logDom = $(".bigpic .w1024 div:first"); //Login_k
    var logDom = $(".Login_k");
    if (logDom.length == 0) {
        logDom = $(".Login_k");
        loginBarCss = "loginbar_now2";
    }
    logDom.html('\
    <div class="' + loginBarCss + '">\
                <div class="loginbar_now_in">\
                    <div class="loginbar_content">\
                        <div class="loginbar_content_in">\
                            <div class="loginbar_name">' + data.realname + '，你好！</div>\
                            <div>\
                                <!--<p>学时：<span class="orange">' + data.studytime + '</span></p>-->\
                                积分：<span class="orange">' + data.score + '</span>分&nbsp;｜&nbsp;总学时：<span class="orange">' + data.studytime + '</span><!--上次登录:<span class="orange">' + data.logindate + '</span>-->\
                            </div>\
                            <div>\
                                第<span class="orange">' + data.logincount + '</span>次登录&nbsp;\
                            </div>\
                            <div>\
                                <a class="btn_enter" href="/study/"><span>我的学习空间</span></a>\
                                <span><a href="#">退出登录</a></span>\
                            </div>\
                        </div>\
                    </div>\
                </div>\
            </div>\
            ');
    logDom.find('a:eq(1)').click(function() {
        window.location.href = "/portal/LogoutSSO.aspx?next=3";

        return false;
    });
}